package com.woniu.communitymanagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.communitymanagement.entity.Carcharge;
import com.woniu.communitymanagement.entity.HttpResult;
import com.woniu.communitymanagement.service.CarchargeService;
import com.woniu.communitymanagement.mapper.CarchargeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author QIQI
* @description 针对表【carcharge(车位收费表)】的数据库操作Service实现
* @createDate 2022-11-30 17:15:19
*/
@Service
public class CarchargeServiceImpl extends ServiceImpl<CarchargeMapper, Carcharge>
    implements CarchargeService{
    @Autowired(required = false)
    private CarchargeMapper carchargeMapper;
    @Override
    public HttpResult updateCarcharge(Carcharge carcharge) {
        HttpResult httpResult=null;
        boolean isUpdate = updateById(carcharge);
        if(isUpdate){
            httpResult=new HttpResult(null,null,200,"修改成功");
        }else{
            httpResult=new HttpResult(null,null,500,"修改失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult deleteById(Integer id) {
        HttpResult httpResult=null;
        int count = carchargeMapper.deleteById(id);
        if(count>0){
            httpResult=new HttpResult(null,null,200,"删除成功");
        }else {
            httpResult=new HttpResult(null,null,500,"删除失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult insertOne(Carcharge carcharge) {
        HttpResult httpResult=null;
        int count = carchargeMapper.insert(carcharge);
        if (count>0){
            httpResult=new HttpResult(null,null,200,"增加成功");
        }else {
            httpResult=new HttpResult(null,null,500,"增加失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult selectOneById(Integer id) {
        HttpResult httpResult=null;
        Carcharge carcharge = carchargeMapper.selectOneById(id);
        if(carcharge!=null){
            httpResult=new HttpResult(carcharge,1,200,"查询成功");
        }else{
            httpResult=new HttpResult(null,null,500,"查询失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult getCarChargeList(String parkingNumbers, String carChargeStatus, Integer pageIndex, Integer pageSize) {
        HttpResult httpResult=null;

        List<Carcharge> carcharges = carchargeMapper.getChargelist(parkingNumbers, carChargeStatus, (pageIndex-1)*pageSize, pageSize);
        Integer count = carchargeMapper.selectCount(parkingNumbers, carChargeStatus);
        if(carcharges!=null&&carcharges.size()>0){
            httpResult=new HttpResult(carcharges,count,200,"查询成功");
        }else {
            if(pageIndex>1){
                carcharges = carchargeMapper.getChargelist(parkingNumbers, carChargeStatus, (pageIndex - 2) * pageSize, pageSize);
                if(carcharges!=null&&carcharges.size()>0){
                    httpResult=new HttpResult(carcharges,count,200,"查询成功");
                }else{
                    httpResult=new HttpResult(null,null,500,"查询失败");
                }
            }
        }
        return httpResult;
    }

    @Override
    public HttpResult getall() {
        HttpResult httpResult=null;
        List<Carcharge> carcharges = carchargeMapper.getall();
        if(carcharges!=null&&carcharges.size()>0){
            httpResult=new HttpResult(carcharges,carcharges.size(),200,"查询成功");
        }else{
            httpResult=new HttpResult(null,null,500,"查询失败");
        }
        return httpResult;
    }
}




